﻿Imports Runa.UI.Input
Imports Runa.UI.Styles
Imports Microsoft.Xna.Framework
Imports Microsoft.Xna.Framework.Graphics
Imports System.Collections.ObjectModel
Imports System.Collections.Specialized

Namespace Global.Runa.UI.Controls

    Public Enum SelectionMode
        MultiExtended = 3
        MultiSimple = 2
        None = 0
        One = 1
    End Enum

    Public Class ListBoxItem
        Inherits ContentControl

        Public Sub New(ByVal g As Game, ByVal value As String)
            MyBase.New(g)
            Me.Content = New Label(g) With {.Text = value}
        End Sub

        Public Sub New(ByVal item As UIElementBase)
            MyBase.New(item.Game)
            Me.Content = item
        End Sub

        Protected Overrides Sub Initialize()           
            MyBase.Initialize()
        End Sub

        Public Overrides Sub Draw(SpriteBatch As Microsoft.Xna.Framework.Graphics.SpriteBatch)
            MyBase.Draw(SpriteBatch)
        End Sub

        Public Overrides Sub Update(gameTime As Microsoft.Xna.Framework.GameTime)
            MyBase.Update(gameTime)
        End Sub

        Public Overrides Sub UpdateLayout()
            MyBase.UpdateLayout()
        End Sub

        ''' <summary>
        ''' In fact, I don't know what to do with this right now...
        ''' </summary>
        ''' <param name="sender"></param>
        ''' <param name="e"></param>
        ''' <remarks></remarks>
        Private Sub ListBoxItem_BoundsChange(sender As Object, e As System.EventArgs) Handles Me.BoundsChange

        End Sub

    End Class

    Public Class ListBox
        Inherits Panel

        Public Event SelectionChanged(ByVal sender As Object, ByVal e As EventArgs(Of Object))
        Public Event ItemClick(ByVal sender As Object, ByVal e As EventArgs(Of Object))
        Public Event ItemDblClick(ByVal sender As Object, ByVal e As EventArgs(Of Object))

        Public Property ItemsSource() As ObservableCollection(Of Object)
        Public Property SelectedItem() As Object
        Public Property SelectedItems() As IEnumerable(Of Object)
        Public Property ScrollAlwaysVisible As Boolean = True
        Public Property SelectionMode As SelectionMode

        Public Property Items() As New List(Of ListBoxItem)

        Private m_scrollBar As ScrollBar

        Public Sub New(ByVal Parent As UIElementBase)
            MyBase.New("", Parent)
            Me.Name = Me.GetType().Name & "_" & BytesToString(Guid.NewGuid().ToByteArray())
        End Sub

        Public Sub New(ByVal Name As String, ByVal Parent As UIElementBase)
            MyBase.New(Name, Parent)
        End Sub

        Protected Overrides Sub Initialize()
            Me.m_scrollBar = New VScrollBar(Me)
            Me.m_scrollBar.Location = New Vector2(Me.Size.X, 0)
            AddHandler Me.ItemsSource.CollectionChanged, AddressOf ItemsSource_CollectionChanged
            MyBase.Initialize()
        End Sub

        Public Overrides Sub Draw(SpriteBatch As Microsoft.Xna.Framework.Graphics.SpriteBatch)
            MyBase.Draw(SpriteBatch)
        End Sub

        Public Overrides Sub Update(gameTime As Microsoft.Xna.Framework.GameTime)
            MyBase.Update(gameTime)
        End Sub

        Public Overrides Sub UpdateLayout()
            Me.m_scrollBar.Location = New Vector2(Me.Size.X, 0)
            Dim pos = Me.m_scrollBar.Value

            MyBase.UpdateLayout()
        End Sub

        Private Sub ItemsSource_CollectionChanged(ByVal sender As Object, ByVal e As NotifyCollectionChangedEventArgs)
            'Dim collection = CType(sender, ObservableCollection(Of ListBoxItem))
            'Select Case e.Action
            '    Case NotifyCollectionChangedAction.Add
            '        For Each item As ListBoxItem In e.NewItems

            '        Next
            '    Case NotifyCollectionChangedAction.Move
            '    Case NotifyCollectionChangedAction.Remove
            '    Case NotifyCollectionChangedAction.Replace
            '    Case NotifyCollectionChangedAction.Reset
            'End Select
            Me.UpdateLayout()
        End Sub

    End Class

End Namespace